﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace GameCell.ConfigDataMaker
{
    public static class IEnumerableDbDataEx
    {
        public static void SaveToFile<T>(this IEnumerable<T> source)
        {
            Directory.CreateDirectory("configs");
            string filename = "configs/" + typeof(T).Name + ".dat";

            Stream fStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);

            BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器

            binFormat.Serialize(fStream, source);
            fStream.Close();
        }

        public static void SaveToDb<T>(this IEnumerable<T> source, string connStr)
        {
            using (var conn = new MySqlConnection(connStr))
            {
                DbDataUtil.CreateStructure<T>(conn);
                DbDataUtil.Save(conn, source);
            }
        }
    }

}
